package it.controllocredito.frontend.activities;

import java.util.ArrayList;
import java.util.List;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.bean.RegistroChiamate;
import it.controllocredito.backend.phone.calllog.ManagerCallLogHandler;
import it.controllocredito.frontend.util.RegistroChiamateArrayAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class RegistroChiamateActivity extends SherlockFragmentActivity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.registro_chiamate_activity);

		CalllogPersistenceManager calllogPersistenceManager = new CalllogPersistenceManager(this.getApplicationContext());
		List<RegistroChiamate> listaRegistroChiamate = calllogPersistenceManager.getRecordsRegistroChiamate();
		
		RegistroChiamate[] x = new RegistroChiamate[listaRegistroChiamate
		                            				.size()];

		int i = 0;

		for (RegistroChiamate registroChiamata : listaRegistroChiamate) {
			x[i] = registroChiamata;
			i++;
		}
		
		RegistroChiamateArrayAdapter registroChiamateArrayAdapter = new RegistroChiamateArrayAdapter(
				getApplicationContext(), x);

		final ListView listView = (ListView) findViewById(R.id.mylist);

		// Assign adapter to ListView
		listView.setAdapter(registroChiamateArrayAdapter);

		// FINE LISTVIEW
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
	        case android.R.id.home:
	            // app icon in action bar clicked; go home
	            Intent intent = new Intent(this, MainFragmentActivity.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

}